import { Member } from './member-model.js';
import { Center } from '../center/center-model.js';
import { Sms } from '../../utils/sms.js';
var member = new Member();
var center = new Center();
var sms = new Sms();
Page({
  data: {
    show_send_code: true,
    countDownNum: 60,
    args: {
      mobile:'',
      pay_pwd:'',
      verifyPassword:'',
      verifyCode:''
    },
    btnType: 'disable',
    btnText: "设置",
    onLoadding: false
  },
  onLoad: function (options) {
    //如果从订单入口进来，获取订单参数，以便设置完成后跳转回去
    this.data.type = options.type ? options.type : '';
    this.data.id = options.id ? options.id : '';

    this.getinfo();
  },
  //获取用户信息
  getinfo: function () {
    var that = this;
    var params = {};
    wx.showLoading({
      title: '加载中...',
    })
    member.info(params, (data) => {
      wx.hideLoading();
      that.setData({
        user : data.item,
      })
      that.data.args.mobile = data.item.mobile;
    });
  },
  //提交表单
  formSubmit: function (e) {
    if (!this.data.args.verifyCode) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (!this.data.args.pay_pwd) {
      wx.showToast({
        title: '请输入支付密码',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (!this.data.args.verifyPassword) {
      wx.showToast({
        title: '请输入确认密码',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.args.pay_pwd != this.data.args.verifyPassword) {
      wx.showToast({
        title: '密码和确认密码不一致',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.sub();
  },
  //设置密码
  sub: function () {
    var that = this;
    var params = that.data.args;
    if (this.data.onLoadding) {
      return false;
    }
    this.setData({ btnText: '正在设置...' })
    this.data.onLoadding = true;
    member.resetPaypwd(params, (data) => {
      that.data.onLoadding = false;
      this.setData({ btnText: '设置' })
      if (data.status == 1) {
        wx.showToast({
          title: "支付密码设置成功",
          icon: 'success',
          duration: 2000, 
          success: function () {
            if (that.data.type == 1) {
              var url = "../order/payment?order_id="+that.data.id;
            } else if (that.data.type == 2) {
              var url = "../order/payment?parent_sn=" + that.data.id;
            }
            else {
              var url = '../center/center';
            }
            setTimeout(function () {
              wx.navigateTo({
                url: url
              });
            }, 2000)
          }
        })
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  //发送短信验证码
  sendCode: function (e) {
    if (!this.data.args.mobile) {
      wx.showToast({
        title: '请先填写手机号码',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.setData({
      show_send_code: false,
      countDownNum: 60
    });
    this.countDown();
    var params = {
      mobile: this.data.args.mobile,
      scene: 7
    }
    sms.send(params, (data) => {
      if (data.status == 1) {
        wx.showToast({
          title: "验证码已发送到您手机上",
          icon: 'none',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  countDown: function () {
    let that = this;
    let countDownNum = that.data.countDownNum;//获取倒计时初始值
    //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
    that.setData({
      timer: setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
        //每隔一秒countDownNum就减一，实现同步
        countDownNum--;
        that.setData({
          countDownNum: countDownNum
        })
        //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
        if (countDownNum == 0) {
          clearInterval(that.data.timer);
          that.setData({
            show_send_code: true
          });
        }
      }, 1000)
    })
  },
  //获取用户输入的密码
  passwordChange: function (e) {
    var val = e.detail.value;
    this.data.args.pay_pwd = val
    this.btnChange();
  },
  //获取用户输入的确认密码
  repasswordChange: function (e) {
    var val = e.detail.value;
    this.data.args.verifyPassword = val
    this.btnChange();
  },
  //获取用户输入的验证码内容
  codeChange: function (e) {
    var val = e.detail.value;
    this.data.args.verifyCode = val
    this.btnChange();
  },
  //判断激活按钮的样式
  btnChange: function () {
    let type = 'disable'
    if (this.data.args.mobile && this.data.args.verifyCode && this.data.args.pay_pwd && this.data.args.verifyPassword) {
      type = 'sub';
    }
    this.setData({
      btnType: type
    });
  }
})